package com.example.administrator.x2picturetransliteration.Util.Payment;

import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.example.administrator.x2picturetransliteration.Constants;
import com.example.administrator.x2picturetransliteration.Util.ToastUtil;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Map;

/**
 * 微信支付
 * Created by Administrator on 2018/9/20.
 */

public class WeChatUtil {

    private IWXAPI msgApi;

    private AppCompatActivity activity;

    public WeChatUtil(AppCompatActivity activity){

        this.activity=activity;

        msgApi = WXAPIFactory.createWXAPI(activity, Constants.getConstants().getAppId(), true);
        msgApi.registerApp(Constants.getConstants().getAppId());

    }

    public void Payment(final Map<String, String> Param){

        if(!msgApi.isWXAppInstalled()&& !msgApi.isWXAppInstalled()) {
            new ToastUtil(activity).makeText(activity,"请您先安装微信客户端",Toast.LENGTH_SHORT).show();
            return;
        }

        new Thread(new Runnable() {
            @Override
            public void run() {


                PayReq  request  = new PayReq ();
                request.appId = Param.get("appid");
                request.partnerId = Param.get("partnerid");
                request.prepayId= Param.get("prepayid");
                request.nonceStr= Param.get("noncestr");
                request.timeStamp=  Param.get("timestamp");
                request.packageValue = Param.get("package");
                request.sign= Param.get("sign");
                msgApi.sendReq(request);



                System.out.println("------------------------------appid:"+Param.get("appid"));
                System.out.println("------------------------------partnerid:"+Param.get("partnerid"));
                System.out.println("------------------------------prepayid:"+Param.get("prepayid"));
                System.out.println("------------------------------package:"+Param.get("package"));
                System.out.println("------------------------------noncestr:"+Param.get("noncestr"));
                System.out.println("------------------------------timestamp:"+Param.get("timestamp"));
                System.out.println("------------------------------paySign:"+Param.get("sign"));

            }
        }).start();

    }


}
